<?php
	session_start();

	require_once("includes/connect.php");
	require_once("includes/function.php");

	$panngan2 = $_REQUEST["panngan2"];
	$panngan3 = $_REQUEST["panngan3"];
?>

<script type="text/javascript">
	$(document).ready(function(){

		$("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
			submitSuccess: function ($form, event) { 
				//event.preventDefault();

			}
		});

		$( "#panngan2" ).change(function() {
			var panngan2= $(this).val();
			var panngan3 = "<?php echo $panngan3; ?>";
			
			if (panngan2 != "") {
				$.post('dd_panngan.php', {  panngan2: panngan2, panngan3: panngan3}, function(data) { 
					$("#dd_panngan3").html(data); 
						//alert(data);
					} );
			}
		});

		$( "#panngan2" ).trigger("change");

		$("#month_year").datepicker({
			dateFormat: "yy-mm"
			
		});
	});
</script>

<?php
	

	//echo $panngan3;

	$submit = $_REQUEST["submit"];

	$budget_year = $_REQUEST["budget_year"];

	$month_year = $_REQUEST["month_year"];

?>

<form id="frm_init_pay" name="frm_init_pay" method="post" action="index.php" novalidate="">
	<input type="hidden" name="pages" value="frm_init_pay">
	<div class="row-fluid">
		<div class="control-group span1">
			<label class="control-label" for="budget_year">ปีงบประมาณ</label>
			<div class="controls ">
				<select class="span12" id="budget_year" name="budget_year" readonly>
				<option>2556</option>
				<option>2557</option>
				<option>2558</option>
				<option>2559</option>
				<option>2560</option>
				</select>
			</div>
		</div>

		<div class="control-group span4">
			<label class="control-label" for="panngan">แผนงาน</label>
			<div class="controls ">
				<select class="span12" id="panngan2" name="panngan2" required>
				<option value="">เลือก...</option>
				<?php
					$db = NEW myDBS;
					$db->selectDB("hr_udoncity");

					$sql = "SELECT DISTINCT panngan2_id 
							FROM position 
							WHERE lvl_id like '" .$_SESSION["lvl1_id"]. "%'
							AND panngan2_id IS NOT NULL"
							; 

					$result = $db->runSQL($sql);

					while($rows = $db->fetch_object ( $result )){
						$db->selectDB("account");
						
						$sql = "SELECT DISTINCT * 
								FROM panngan2 
								WHERE panngan2_id = '" .$rows->panngan2_id. "'"
								;

						$result1 = $db->runSQL($sql);
						while($rows1 = $db->fetch_object ( $result1 )){
							if ($rows1->panngan2_id == $panngan2) {
								$selected = " selected";
							} else {
								$selected = "";
							}
							echo "<option value='".$rows1->panngan2_id."' $selected>$rows1->panngan2_name</option>";
						}
					}

				?>
				</select>
			</div>
		</div><!--/span-->

		<div class="control-group span3">
			<label class="control-label" for="panngan3">งาน</label>
			<div class="controls " id="dd_panngan3">
				<select class="span12" id="panngan3" name="panngan3" required>
					
					<option value="">เลือก...</option>
					

				</select>
			</div>
		</div><!--/span-->

		<div class="control-group span2">
			<label class="control-label" for="month_year">เดือน/ปี</label>
			<div class="controls">
				<input class="span12" type="text" id="month_year" name="month_year" value="<?php echo $month_year; ?>" required />
            </div>
		</div>

		<div class="control-group span2 ">
			<label class="control-label">&nbsp;</label>
			<div class="controls  ">
				<button type="submit" id="submit" name="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i> ตกลง</button>
				<a href="index.php?pages=home" role="button" class="btn"><i class="icon-remove"></i>ยกเลิก</a>
            </div>
		</div>
	</div><!-- <div class="row-fluid"> -->


</form>

<script type="text/javascript">
	$(document).ready(function(){
		$('#example').dataTable({
			"iDisplayStart": "<?php echo 0; ?>",
			"iDisplayLength": 100
		});
	});
	
	function edit_init(id, salary) {
				
		var month_year = "<?php echo $month_year; ?>";
		var panngan2 = "<?php echo $panngan2; ?>";
		var panngan3 = "<?php echo $panngan3; ?>";
		var budget_year = "<?php echo $budget_year; ?>";

		$('<div>').dialog2({
			autoOpen: true,
			title: "แก้ไขข้อมูลเงินเดือน",
			content: "dlg_edit_pay.php?id=" + id + "&month_year=" + month_year + "&panngan2=" + panngan2 + "&panngan3=" + panngan3 + "&salary=" + salary + "&budget_year=" + budget_year,
			id: "modal-edit-pay",
			//	height:140
			//resizable: false,
			//width:1000
		});

	}

	function info_salary(id) {
		//alert(id);
		var month_year = "<?php echo $month_year; ?>";
		var panngan2 = "<?php echo $panngan2; ?>";
		var panngan3 = "<?php echo $panngan3; ?>";
		var budget_year = "<?php echo $budget_year; ?>";

		$('<div>').dialog2({
			autoOpen: true,
			title: "ข้อมูลเงินเดือน",
			content: "info_salary.php?id=" + id + "&month_year=" + month_year + "&panngan2=" + panngan2 + "&panngan3=" + panngan3 + "&budget_year=" + budget_year,
			id: "modal-info-salary",
			//	height:140
			//resizable: false,
			//width:1000
		});
	}
</script>
<?php
	if (isset($submit)) {
		
		$db->selectDB("hr_udoncity");

		$sql = "SELECT position.id, MAX( position_history_date ) 
				FROM position, position_history
				WHERE position_history.id = position.id
				AND panngan2_id = '$panngan2' 
				AND panngan3_id = '$panngan3'
				GROUP BY position.id
				";

		//echo $sql;
		$results = $db->runSQL($sql);
?>
	<table id="example" class="table table-bordered" width="100%" >
		<caption><br></caption>
		<thead>
			<tr>
				<th>#</th>
				<th>เลขประจำตัว</th>
				<th>ชื่อ - นามสกุล</th>
				<th>เงินเดือน</th>
				<th>action</th>
				<th>ข้อมูล</th>
			</tr>
		</thead>
		<tbody>
<?php
		$i=0;
		
		while ($rows = $db->fetch_object($results)) {
			$sql = "SELECT person.id, fname, lname, salary FROM person, position_history
					WHERE position_history.id = person.id
					AND person.id = '". $rows->id ."'
					";
			$results1 = $db->runSQL($sql);

			$rows1 = $db->fetch_object($results1);
			$i++;
			echo "<tr>
					<td>$i</td>
				  <td>$rows1->id</td>
				  <td>$rows1->fname&nbsp;&nbsp;&nbsp;$rows1->lname</td>
				  <td align='right'>".number_format($rows1->salary, 2)."</td>
				  <td><button type='button' class='btn' onclick='javascript:edit_init($rows1->id, $rows1->salary)'>Click</button></td>
				  ";
			
			$sql = "SELECT * FROM tbl_init
			WHERE card_id = '$rows1->id'
			AND panngan2_id = '$panngan2'
			AND panngan3_id = '$panngan3'
			AND month_year = '$month_year'
			AND budget_year = '$budget_year'

			";
			//$db1->selectDB("account");
			//$results3 = $db1->runSQL($sql);

			//$numrows = $db1->num_rows($results3);
			
				echo "<td><a href='javascript:info_salary($rows1->id)'>ดู</a></td>
				  </tr>
				  ";

		}
?>
	</tbody>
	</table>
<?php
	}
?>
